package com.kalix.ynpetroleum.warning.biz;

import com.kalix.framework.core.util.ConfigUtil;
import com.kalix.ynpetroleum.warning.api.biz.IWarningBeanService;
import com.kalix.ynpetroleum.warning.api.model.InfUtil;
import org.json.JSONArray;
import org.json.JSONObject;


/**
 *@ Author  ZhaoHang
 * @Date  2019/11/18
 **/
public class WarningBeanServiceImpl implements IWarningBeanService {
    public static String Ip = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ynpetroleum.portconf");
    public static String Port =(String)ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ynpetroleum.portconf");
    public String getWarning() {
        String url = "http://"+Ip+":"+Port+"/query/mo/AlarmEvent?where=(alarmStatus=2%20or%20alarmStatus=3)";
        String back= InfUtil.interfaceUtil(url,"");
        JSONArray array  = new JSONArray(back);
        JSONArray data = new JSONArray();
        for (int i = 0; i < array.length(); i++) {
            JSONObject ob  = (JSONObject) array.get(i);
            String levelValue = "";
            JSONArray properties = ob.getJSONArray("properties");
            String level = getLevel(properties);
            if ("1".equals(level)){
                levelValue = "提示";
            }else if("2".equals(level)) {
                levelValue = "低级";
            }else if("3".equals(level)) {
                levelValue = "中级";
            }else if("4".equals(level)) {
                levelValue = "高级";
            }else if("5".equals(level)) {
                levelValue = "紧急";
            }
            String alarmContext = getAlarmContext(properties);
            JSONObject obj = new JSONObject();
            obj.put("level",levelValue); // 告警级别
            obj.put("location",alarmContext); // 告警描述
            data.put(obj);
        }
        JSONObject jsonObject = new JSONObject();
        JSONObject ject3 = new JSONObject();
        ject3.put("name","告警级别");
        ject3.put("width","150px");
        JSONObject ject4 = new JSONObject();
        ject4.put("name","告警描述");
        ject4.put("width","946px");
        JSONArray arr =new JSONArray();
        arr.put(ject3);
        arr.put(ject4);
        jsonObject.put("title","当前警告信息");
        jsonObject.put("thead",arr);
        jsonObject.put("data",data);
        return jsonObject.toString();
    }
    public String getLevel(JSONArray properties){
        String str = "";
        for (int i = 0; i < properties.length(); i++) {
            JSONObject obj  = (JSONObject) properties.get(i);
            if("level".equals(obj.getString("name"))){
                Object value = obj.getJSONObject("value").get("value");
                if (value instanceof Integer) {
                    str = String.valueOf(value);
                }
            }
        }
        return str;
    }
    public String getAlarmContext(JSONArray properties){
        String str = "";
        for (int i = 0; i < properties.length(); i++) {
            JSONObject obj  = (JSONObject) properties.get(i);
            if("alarmContext".equals(obj.getString("name"))){
                Object value = obj.getJSONObject("value").get("value");
                if (value instanceof String) {
                    str = String.valueOf(value);
                }
            }
        }
        return str;
    }
}
